package admin_admin

import (
	"gitee.com/theegg/go-brick/app/controller/admin_api"
	"gitee.com/theegg/go-brick/app/model/admin/admin_users"
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/cstruct"
	"github.com/gin-gonic/gin"
)

// 日志列表
func AdminLogList(c *gin.Context) {
	// 检验参数
	var validaParams admin_users.AdminUsersQueryParams
	if errCheck := c.ShouldBind(&validaParams); errCheck != nil {
		app.NewResponse(c).JsonReturn(false, app.NewError(errCheck).Error(), nil)
		return
	}

	// 判定当前登陆用户是否存在
	currentAdminUser, errCurrent := admin_api.GetLoginAdminUser(c)
	if errCurrent != nil {
		app.NewResponse(c).JsonReturn(false, errCurrent.Error(), nil)
		return
	}

	// 获取自身的roleID
	role, errRole := currentAdminUser.Role()
	if errRole != nil {
		app.NewResponse(c).JsonReturn(false, app.NewError(errRole).Error(), nil)
		return
	}

	// 获取数据
	data, err := admin_users.GetDataList(cstruct.StructToMap(validaParams), *role)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, app.NewError(err).Error(), nil)
		return
	}

	// 返回选项
	childRoleOption, errOption := role.GetChildRoleOption(true)
	if errOption != nil {
		app.NewResponse(c).JsonReturn(false, app.NewError(errOption).Error(), nil)
		return
	}

	data["role_option"] = childRoleOption

	app.NewResponse(c).JsonReturn(true, "获取数据成功", data)
	return
}

// 日志详情
func AdminLogDetail(c *gin.Context) {
	app.NewResponse(c).JsonReturn(true, "获取数据成功", nil)
	return
}
